package com.iwop.moto.controller;

import com.iwop.moto.auth.LoginAuth;
import com.iwop.moto.comm.ErrCode;
import com.iwop.moto.model.MBankAccount;
import com.iwop.moto.model.MBankList;
import com.iwop.moto.model.ResultModel;
import com.iwop.moto.service.MBankListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by mobom on 2016/9/30.
 */

@Controller
public class MBankListController extends BaseController{

    @Autowired
    private MBankListService mBankListService;

    @RequestMapping("/api/bank/list/add")
    @ResponseBody
    @LoginAuth(isval=true)
    public ResultModel bankAccountAdd(@ModelAttribute MBankList mBankList,HttpServletRequest request){
        mBankList.setSid(UUID.randomUUID().toString());
        mBankList.setStatusTime(new Date());
        mBankList.setAddTime(new Date());
        mBankList.setUserId(request.getParameter("session_id"));
        mBankList.setStatusFlag("1");
        try {
            mBankListService.insertMBankList(mBankList);
            return getSucceed(ErrCode.SUCCEED);
        }catch (Exception e){
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    @RequestMapping("/api/bank/list/edit")
    @ResponseBody
    @LoginAuth(isval=true)
    public ResultModel bankAccountEdit(@ModelAttribute MBankList mBankList,HttpServletRequest request){
        mBankList.setStatusTime(new Date());
        try {
            mBankListService.upMBankList(mBankList);
            return getSucceed(ErrCode.SUCCEED);
        }catch (Exception e){
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    /**
     * @功能: 获取用户认证银行卡
     * @日期: gewei 2016-09-30 19:52 create
     * @参数: session_id 用户id
     */
    @RequestMapping("/api/bank/list/get")
    @ResponseBody
    @LoginAuth(isval=true)
    public ResultModel getBankAccount(HttpServletRequest request){
        try {
            String userId = request.getParameter("session_id");
            List mBankList = mBankListService.getMBankList(userId);
            if(mBankList==null || mBankList.size()==0){
                return getSucceed(null,ErrCode.ERROR_NOCOUNT);
            }else{
                return getSucceed(mBankList,ErrCode.SUCCEED);
            }
        }catch (Exception e){
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    @RequestMapping("/api/bank/list/detail")
    @ResponseBody
    @LoginAuth(isval=true)
    public ResultModel getBankAccountDetail(HttpServletRequest request){
        try {
            String userId = request.getParameter("sid");
            MBankList mBankListDetail = mBankListService.getMBankListDetail(userId);
            return getSucceed(mBankListDetail,ErrCode.SUCCEED);
        }catch (Exception e){
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }
}
